//
//  StoneShowDetailViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/19.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "StoneShowDetailViewController.h"
#import "StoneShowMoreViewController.h"
@interface StoneShowDetailViewController ()<BestScrollViewDelegate,BestScrollViewDatasource>
@property (weak, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UILabel *stoneName;
@property (weak, nonatomic) IBOutlet UITextView *descTextView;
@property (weak, nonatomic) IBOutlet UILabel *productLabel;
@property (weak, nonatomic) IBOutlet UILabel *featureLabel;
@property (weak, nonatomic) IBOutlet UILabel *payType;
@property (weak, nonatomic) IBOutlet UILabel *moreLabel;
@property (weak, nonatomic) IBOutlet UIButton *collcetButton;
@property (nonatomic,strong)  BestScrollView *headerScroll;//滚动栏
@property (nonatomic,strong)  NSMutableArray  *imageArray;
@property(nonatomic,assign)CGFloat score;
@property(nonatomic,assign)CGFloat needScore;
@property(nonatomic,assign)NSInteger ifSee;
@end

@implementation StoneShowDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSMutableArray array];

    [self initViewLayout];
    [self getPersonScore];
    [self getStoneDetail];
    [self getStoneBanner];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 569), NO);

    _headerScroll = [[BestScrollView alloc]  initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 155), NO)];
    [self.scroView insertSubview:_headerScroll belowSubview:_collcetButton];

}

- (IBAction)back:(id)sender {
    
    [self backBtn];
}

#pragma mark ===  查询用户积分
- (void)getPersonScore{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    self.shareApi.delegate=self;
    
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryScoreInfo withHttpType:@"POST" withTag:GS_GS_queryScoreInfo];
}

#pragma mark === 收藏
- (IBAction)collect:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_stoneId forKey:@"msfoId"];
    self.shareApi.delegate=self;
    
    [self.shareApi  requestOperationWithDic:dic withPath:GS_saveUpdateCollectStatus withHttpType:@"POST" withTag:GS_GS_saveUpdateCollectStatus];
  
}
#pragma mark === 查看更多
- (IBAction)seeMore:(id)sender {
    if (_ifSee == 0) {//没看过
        if (_score<_needScore) {
            UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"积分不足！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else{
            UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            StoneShowMoreViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"StoneShowMore"];
            page.stoneId = _stoneId;
            [[Common getNavigation]pushViewController:page];
        }
    }else{
        UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        StoneShowMoreViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"StoneShowMore"];
        page.stoneId = _stoneId;
        [[Common getNavigation]pushViewController:page];
    }
   
}


#pragma mark == 矿山详情
- (void)getStoneDetail{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
   
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];

    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_stoneId forKey:@"msfoId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryStoreForeignDetail withHttpType:@"POST" withTag:GS_GS_queryStoreForeignDetail];
    
}

#pragma mark == 矿山滚动栏信息
- (void)getStoneBanner{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_stoneId forKey:@"msfoId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryStoreForeignImage withHttpType:@"POST" withTag:GS_GS_queryStoreForeignImage];
}


#pragma  mark BestView Delegate Data=source=====================


- (NSInteger)numberOfPages
{
    
    return  self.imageArray.count;
    
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    
    if (self.imageArray.count>0) {
        
        UIImageView  *imageView=[[UIImageView alloc]  initWithFrame: flexibleFrame(CGRectMake(0, 0, 320, 155), NO)];
        [imageView  sd_setImageWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,_imageArray[index]]] placeholderImage:[UIImage imageNamed:@"beijing"]];
        return imageView;
    }
    return nil;
}


- (void)didClickPage:(BestScrollView *)csView atIndex:(NSInteger)index
{
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == GS_GS_queryStoreForeignDetail) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary *dic= database[@"result"];
            NSString * collect = [NSString stringWithFormat:@"%@",dic[@"collection"]];
            if ([collect isEqualToString:@"1"]) {
                _collcetButton.selected = YES;
            }else{
                _collcetButton.selected = NO;
                
            }
            
            _ifSee = [dic[@"log"]integerValue];
            
            dic = dic[@"store"];
            _stoneName.text = [NSString stringWithFormat:@"%@",dic[@"msfoName"]];
            _descTextView.text = [NSString stringWithFormat:@"%@",dic[@"msfoDescription"]];
            _productLabel.text = [NSString stringWithFormat:@"%@",dic[@"msfoProduct"]];
            _featureLabel.text = [NSString stringWithFormat:@"%@",dic[@"msfoFeature"]];
            _moreLabel.text = [NSString stringWithFormat:@"需%@积分可查看",dic[@"mstoScore"]];
            _payType.text = [NSString stringWithFormat:@"%@",dic[@"msfoTypeName"]];
            _needScore = [dic[@"mstoScore"] floatValue];
            
        }
    }else  if (tag == GS_GS_queryStoreForeignImage) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSArray * array = database[@"result"];
            if (array.count == 0) {
                return;
            }
            NSString * images = [NSString stringWithFormat:@"%@",array[0][@"msfiImage"]];
            self.imageArray = [[images componentsSeparatedByString:@","]mutableCopy];
            if (_imageArray.count==0) {
                return;
            }
            self.headerScroll.delegate=self;
            self.headerScroll.datasource=self;
            [self.headerScroll showImageArray:self.imageArray   withAnimation:YES];

        
        }
    }else if (tag == GS_GS_saveUpdateCollectStatus){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
          
            _collcetButton.selected = !_collcetButton.selected;
        }
    }else if (tag == GS_GS_queryScoreInfo){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            
            _score = [database[@"result"][@"museScore"]floatValue];
        }
    
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
